//
//  ForumViewController.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ForumViewController.h"
#import "FinderViewController.h"
#import "DetailViewController.h"
#import "CommentViewController.h"
#import "DownViewController.h"

#import "ForumTableViewCell.h"
#import "CommentTableViewCell.h"
#import "DownTableViewCell.h"
#import "CarTableViewCell.h"


#import "Forum.h"
#import "Comment.h"
#import "Listing.h"
#import "UpStairSliderView.h"
#import "UIButton+InitButton.h"
#import "ForumList.h"
#import "ForumBrand.h"
#import "ForumCar.h"

@interface ForumViewController ()<UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, UpStairSliderDelegate>
@property(nonatomic, assign)NSInteger page;
@property(nonatomic, retain)NSNumber *pageIndex;
@property(nonatomic, assign)NSInteger num;
@property(nonatomic, retain)UISegmentedControl *segmentControl;
@property(nonatomic, retain)UIScrollView *scrollView;
@property(nonatomic, assign)NSInteger bbsnameNum;

@property(nonatomic, retain)UITableView *tableViewO;
@property(nonatomic, retain)UITableView *tableViewT;
@property(nonatomic, retain)UITableView *tableViewResult;
@property(nonatomic, retain)UITableView *tableViewTheme;
@property(nonatomic, retain)UITableView *tableViewCar;
@property(nonatomic, retain)UITableView *tableViewCarForum;

@property(nonatomic, retain)NSMutableArray *arr;
@property(nonatomic, retain)NSMutableArray *arrAll;
@property(nonatomic, retain)NSMutableArray *arrName;
@property(nonatomic, retain)NSMutableArray *arrId;
@property(nonatomic, retain)NSMutableArray *arrT;
@property(nonatomic, retain)NSMutableArray *arrTheme;
@property(nonatomic, retain)NSMutableArray *arrCar;
@property(nonatomic, retain)NSMutableArray *arrCarFo;
@property(nonatomic, retain)NSMutableArray *arrCarFoTwo;
@property(nonatomic, retain)NSMutableArray *stringArr;
@property(nonatomic, retain)NSMutableArray *stringIdArr;
@property(nonatomic, retain) NSMutableArray *letterArr;

@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain)NSString *strID;

@property(nonatomic, retain)UIView *viewB;
@property(nonatomic, retain)UIView *viewTheme;
@property(nonatomic, retain)UIView *viewCar;
@property(nonatomic, retain)UIView *viewCarForum;





@end

@implementation ForumViewController
- (void)dealloc
{
    [_segmentControl release];
    [_scrollView release];
    [_pageIndex release];
    
    [_tableViewO release];
    [_tableViewT release];
    [_tableViewResult release];
    [_tableViewTheme release];
    [_tableViewCar release];
    [_tableViewCarForum release];
   
    
    [_arrCarFo release];
    [_arrCarFoTwo release];
    [_arr release];
    [_arrAll release];
    [_arrName release];
    [_arrId release];
    [_arrT release];
    [_arrTheme release];
    [_arrCar release];
    [_strID release];
    [_str release];
    [_viewB release];
    [_viewCar release];
    [_viewTheme release];
    [_viewCarForum release];
    
    [super dealloc];
}

//下拉刷新主页面tableView上的数据
- (void)datahander
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:self.str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSMutableArray *listArray = [[result objectForKey:@"result"]objectForKey:@"list"];
        [self.arr removeAllObjects];
        for (NSDictionary *listDic in listArray) {
            Forum *forum = [[Forum alloc]initWithDictionary:listDic];
            [self.arr addObject:forum];
            [forum release];
        }
        [_tableViewO reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);}];
}

//上拉加载
- (void)datahander:(NSString *)strings
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:strings parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSMutableArray *listArray = [[result objectForKey:@"result"]objectForKey:@"list"];
        self.pageIndex =  [[result objectForKey:@"result"] objectForKey:@"pageindex"];
        for (NSDictionary *listDic in listArray) {
            Forum *forum = [[Forum alloc]initWithDictionary:listDic];
            [self.arr addObject:forum];
            [forum release];
        }
        [_tableViewO reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);}];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.num = 0;
    
    self.str = @"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c0-p1-s30.html";
  
    [self datahander];
    
    //刷新数组初始化
    self.arr = [NSMutableArray array];
    
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    //设置segmentControl控制全局
    self.segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"精选日报", @"常用论坛"]];
//    self.segmentControl.tintColor = [UIColor orangeColor];
    self.segmentControl.selectedSegmentIndex = 0;
    self.segmentControl.frame = CGRectMake(5, 20, width - 60 , 30);
    [self.segmentControl addTarget:self action:@selector(clickSegmentcontrolAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentControl];
    [_segmentControl release];
    
    //设置搜索按钮
    UIButton *finderButton = [UIButton buttonWithTitle:nil frame:CGRectMake(width - 40, 20, 30, 30) target:self action:@selector(clickFinderButton:)];
    [finderButton setImage:[UIImage imageNamed:@"sousuo.png"] forState:UIControlStateNormal];
    [self.view addSubview:finderButton];
    
    //设置一个scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, width, height - 50 - 49)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(width * 2, 0);
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    
// 获取plist文件里的数据(name id)
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Car" ofType:@"plist"];
    NSMutableArray *arrForm = [NSMutableArray arrayWithContentsOfFile:path];
    self.arrAll = [NSMutableArray array];
    for (NSDictionary *dic in arrForm) {
        Listing *list = [[Listing alloc]initWithdictionary:dic];
        [self.arrAll addObject:list];

        [list release];
    }
    
    self.arrName = [NSMutableArray array];
    self.arrId = [NSMutableArray array];
    //将name对应的value值放在一个数组里
    for (NSInteger i = 0; i < self.arrAll.count; i++) {
        self.arrName = [self.arrAll valueForKey:@"bbsname"];
        self.arrId = [self.arrAll valueForKey:@"idStr"];
    }
    UpStairSliderView *upStair = [UpStairSliderView upStairWithFrame:CGRectMake(5, 0, width - 10, 30) arr:self.arrName image:[UIImage imageNamed:@"down.png"] action:@selector(clickDownButton:) target:self];
    upStair.rec = 199;
    upStair.delegate = self;
    [self.scrollView addSubview:upStair];
    
    
    //  设置tableView显示内容
    self.tableViewO = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, width, height - 40) style:UITableViewStylePlain];
    self.tableViewO.dataSource = self;
    self.tableViewO.delegate = self;
    [self.tableViewO addHeaderWithTarget:self action:@selector(addHeader:)];
    [self.tableViewO addFooterWithTarget:self action:@selector(addFooter:)];
    [self.tableViewO setHeaderRefreshingText:@"下拉刷新"];
    [self.tableViewO setFooterRefreshingText:@"网络加载中..."];
    [self.scrollView addSubview:self.tableViewO];
    [_tableViewO release];

//  ******************** 常用论坛界面tableViewT 和 加载数据: *****************
    self.tableViewT = [[UITableView alloc]initWithFrame:CGRectMake(width, 5, width, height) style:UITableViewStylePlain];
    self.tableViewT.dataSource = self;
    self.tableViewT.delegate = self;
    [self.scrollView addSubview:self.tableViewT];
    [_tableViewT release];
    NSMutableArray *arrayT = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"车系论坛", @"title", @"为你的爱车找到队伍", @"luckT", @"iconfont-chexilie.png", @"image", nil], [NSMutableDictionary dictionaryWithObjectsAndKeys:@"主题论坛", @"title", @"兴趣爱好的聚集地", @"luckT", @"iconfont-zhutiyou.png", @"image", nil], nil];
    self.arrT = [NSMutableArray array];
    for (NSDictionary *dicT in arrayT) {
        Comment *com = [[Comment alloc]initWithdictionary:dicT];
        [self.arrT addObject:com];
        //下拉侧边栏
        [self downTableView];
        //主题论坛
        [self themeDataHander];
       //车系论坛
        [self carDataHanderAndlayoutViewCar];
        
    }
}


- (void)downTableView
{
    CGFloat width= self.view.bounds.size.width;
    CGFloat height= self.view.bounds.size.height;
    
    self.viewB = [[UIScrollView alloc]initWithFrame:CGRectMake(width / 4, 15, width * 3 / 4, height - 15 - 49)];
    [self.view addSubview:self.viewB];
    self.viewB.backgroundColor = [UIColor whiteColor];
    self.viewB.hidden = YES;
    [self.viewB release];
    CGFloat widthV = self.viewB.frame.size.width;
    CGFloat heightV = self.viewB.frame.size.height;
    
    //给视图添加轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.viewB addGestureRecognizer:swipe];
    [swipe release];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(widthV / 3, 5, widthV / 3, 30)];
    lable.text = @"栏目分类";
    lable.textAlignment = NSTextAlignmentCenter;
    [self.viewB addSubview:lable];
    [lable release];
    
    UIButton *closeButton = [UIButton buttonWithTitle:@"关闭" frame:CGRectMake(widthV  * 2 / 3 + 20, 5 , 40, 30) target:self action:@selector(clickCloseButton:)];
    [self.viewB addSubview:closeButton];
    [closeButton setTitleColor:[UIColor colorFromHexCode:@"224b8f"] forState:UIControlStateNormal];
    
    self.tableViewResult = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, widthV, heightV) style:UITableViewStylePlain];
    self.tableViewResult.dataSource = self;
    self.tableViewResult.delegate = self;
    [self.viewB addSubview:self.tableViewResult];
    [_tableViewResult release];
   
}


//主题论坛详情界面数据获取
- (void)themeDataHander
{ NSString *theme = @"http://app.api.autohome.com.cn/autov4.6/club/clubstheme-a2-pm2-v4.6.1-st635435459960905592.html";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:theme parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSMutableArray *themeArray = [[result objectForKey:@"result"]objectForKey:@"list"];
        self.arrTheme = [NSMutableArray array];
        for (NSDictionary *themeDic in themeArray) {
            Forum *theme = [[Forum alloc]initWithDictionary:themeDic];
            [self.arrTheme addObject:theme];
            [theme release];
        }
         [self.tableViewTheme reloadData];
    //   为主题论坛详情界面布局
        self.viewTheme = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 4, 15, self.view.bounds.size.width * 3 / 4, self.view.bounds.size.height - 49)];
        [self.view addSubview:self.viewTheme];
        self.viewTheme.backgroundColor = [UIColor whiteColor];
        self.viewTheme.hidden = YES;
        [self.viewTheme release];
        CGFloat width= self.viewTheme.bounds.size.width;
        CGFloat height= self.viewTheme.bounds.size.height;
        
        //给视图添加轻扫手势
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
        swipe.direction = UISwipeGestureRecognizerDirectionRight;
        [self.viewTheme addGestureRecognizer:swipe];
        [swipe release];
        
        UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(width / 4, 5, width / 3, 30)];
        lable.text = @"主题论坛";
        lable.textAlignment = NSTextAlignmentCenter;
        [self.viewTheme addSubview:lable];
        [lable release];
        
        UIButton *closeButton = [UIButton buttonWithTitle:@"关闭" frame:CGRectMake(width  * 2 / 3 + 20, 5 , 40, 30) target:self action:@selector(clickCloseButton:)];
        [self.viewTheme addSubview:closeButton];
        [closeButton setTitleColor:[UIColor colorFromHexCode:@"224b8f"] forState:UIControlStateNormal];
        
        self.tableViewTheme = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, width, height) style:UITableViewStylePlain];
        self.tableViewTheme.dataSource = self;
        self.tableViewTheme.delegate = self;
        [self.viewTheme addSubview:self.tableViewTheme];
        [_tableViewTheme release];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);
    }];
}

//   为车系论坛详情界面布局
- (void)carDataHanderAndlayoutViewCar;
{
    [self layoutViewCar];
    NSString *theme = @"http://app.api.autohome.com.cn/autov4.6/club/clubsseries-a2-pm2-v4.6.1-st635436819893664946.html";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:theme parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        NSMutableArray *carArray = [[result objectForKey:@"result"] objectForKey:@"list"];
        self.letterArr = [NSMutableArray array];
          for (NSDictionary *dic in carArray) {
            ForumList *list = [self.letterArr lastObject];
            
            if ([[list letter] isEqualToString:[dic objectForKey:@"letter"]]) {
                ForumBrand *brand = [ForumBrand objectWithDictionary:dic];
                for (NSDictionary *dic in brand.seriesclub) {
                    ForumCar *car = [[ForumCar alloc] initWithDictionary:dic];
                    [brand.fourumCar addObject:car];
                }
                [list.arr addObject:brand];
    
            } else {
                ForumList *list = [[ForumList alloc] init];
                list.letter = [dic objectForKey:@"letter"];
                
                ForumBrand *brand = [ForumBrand objectWithDictionary:dic];
                for (NSDictionary *dic in brand.seriesclub) {
                    ForumCar *car = [[ForumCar alloc] initWithDictionary:dic];
                    [brand.fourumCar addObject:car];
                }
                [list.arr addObject:brand];
                [self.letterArr addObject:list];
                [list release];
            }
        }
            [_tableViewCar reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"失败%@", error);
    }];
}


- (void)layoutViewCar
{
    self.viewCar = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 4, 15, self.view.bounds.size.width * 3 / 4, self.view.bounds.size.height - 64)];
    [self.view addSubview:self.viewCar];
    self.viewCar.backgroundColor = [UIColor whiteColor];
    self.viewCar.hidden = YES;
    [self.viewCar release];
    CGFloat width= self.viewCar.bounds.size.width;
    CGFloat height= self.viewCar.bounds.size.height;
    
    //给视图添加轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.viewCar addGestureRecognizer:swipe];
    [swipe release];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(width / 3, 5, width / 3, 30)];
    lable.text = @"车系论坛";
    lable.textAlignment = NSTextAlignmentCenter;
    [self.viewCar addSubview:lable];
    [lable release];
    
    UIButton *closeButton = [UIButton buttonWithTitle:@"关闭" frame:CGRectMake(width  * 2 / 3 + 20, 5 , 40, 30) target:self action:@selector(clickCloseButton:)];
    [self.viewCar addSubview:closeButton];
    [closeButton setTitleColor:[UIColor colorFromHexCode:@"224b8f"] forState:UIControlStateNormal];
    
    self.tableViewCar = [[UITableView alloc]initWithFrame:CGRectMake(10, 50, width - 10, height - 50) style:UITableViewStylePlain];
    self.tableViewCar.dataSource = self;
    self.tableViewCar.delegate = self;
    [self.viewCar addSubview:self.tableViewCar];
    [_tableViewCar release];
   
}


- (void)layoutViewCarForum
{
    self.viewCarForum = [[UIScrollView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 4, 15, self.view.bounds.size.width * 3 / 4, self.view.bounds.size.height - 49)];
    [self.view addSubview:self.viewCarForum];
    self.viewCarForum.backgroundColor = [UIColor whiteColor];
    self.viewCarForum.hidden = YES;
    [self.viewCarForum release];
    CGFloat width= self.viewCarForum.bounds.size.width;
    CGFloat height= self.viewCarForum.bounds.size.height;
    
    //给视图添加轻扫手势
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.viewCarForum addGestureRecognizer:swipe];
    [swipe release];
    
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(width / 3, 0, width / 3, 30)];
    lable.text = @"车系论坛";
    lable.textAlignment = NSTextAlignmentCenter;
    [self.viewCarForum addSubview:lable];
    [lable release];
    
    UIButton *closeButton = [UIButton buttonWithTitle:@"关闭" frame:CGRectMake(width  * 2 / 3 + 20, 5 , 40, 30) target:self action:@selector(clickCloseButton:)];
    [self.viewCarForum addSubview:closeButton];
    [closeButton setTitleColor:[UIColor colorFromHexCode:@"224b8f"] forState:UIControlStateNormal];
    
    UIButton *reButton = [UIButton buttonWithTitle:@"返回" frame:CGRectMake(20, 5 , 40, 30) target:self action:@selector(clickReButton:)];
    [self.viewCarForum addSubview:reButton];
    [reButton setTitleColor:[UIColor colorFromHexCode:@"224b8f"] forState:UIControlStateNormal];
    
    
    self.tableViewCarForum = [[UITableView alloc]initWithFrame:CGRectMake(10, 45, width - 10, height - 45) style:UITableViewStylePlain];
    self.tableViewCarForum.dataSource = self;
    self.tableViewCarForum.delegate = self;
    [self.viewCarForum addSubview:self.tableViewCarForum];
    [_tableViewCarForum release];
    [_tableViewCarForum reloadData];
  
}

#pragma mark- 下面全是方法:
#pragma mark - tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_tableViewO == tableView) {
        return self.arr.count;
    }else if (_tableViewT == tableView) {
        return self.arrT.count;
    }else if (_tableViewResult == tableView) {
        return self.arrAll.count;
    }else if (_tableViewTheme == tableView) {
        return self.arrTheme.count;
    }else if (_tableViewCar == tableView){
        ForumList *car = [self.letterArr objectAtIndex:section];
        return car.arr.count;
    }else if (_tableViewCarForum == tableView){
        return self.bbsnameNum;
    }else {
        return 0;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    // 当在精选日报界面时:
    if (_tableViewO == tableView) {
        ForumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[ForumTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        Forum *forum = [self.arr objectAtIndex:indexPath.row];
        cell.forum = forum;
        return cell;
        
   // 当在侧面下拉视图时:
    }else if (_tableViewResult == tableView){
        DownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[DownTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        NSString *name = [[self.arrAll objectAtIndex:indexPath.row]valueForKey:@"bbsname"];
        cell.textLabel.text = name;
        return cell;
  // 当在常用论坛界面时:
    }else if (_tableViewT == tableView){
        CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[CommentTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        Comment *com = [self.arrT objectAtIndex:indexPath.row];
        cell.comment = com;
        return cell;
        
  //当在主题论坛-详情界面
    }else if (_tableViewTheme == tableView){
        DownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[DownTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        Forum *them = [self.arrTheme objectAtIndex:indexPath.row];
        cell.forum = them;
        return cell;
 //  当在车系论坛->详情界面
    }else if(_tableViewCar == tableView) {
        static NSString *reuseId = @"zzm";
        CarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
        if (cell == nil) {
            cell = [[[CarTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        ForumList *car = [self.letterArr objectAtIndex:indexPath.section];
        cell.car = [car.arr objectAtIndex:indexPath.row];
        return cell;
    }else if (_tableViewCarForum == tableView){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str]autorelease];
        }
        cell.textLabel.text = [self.stringArr objectAtIndex:indexPath.row];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carFace"];
        if (!cell) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"carFace"] autorelease];
        }
        return cell;
    }
}
//设置分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_tableViewCar == tableView) {
        return self.letterArr.count;
    }else{
        return 1;
    }
}

//设置侧边栏各区名称
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    if (_tableViewCar == tableView) {
        NSMutableArray *array = [NSMutableArray array];
        for (NSInteger i = 0; i < self.letterArr.count; i++) {
            ForumList *car = [self.letterArr objectAtIndex:i];
            NSString *str = car.letter;
            [array addObject:str];
        }
        return array;
    }return nil;
}

//设置分区顶部的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (_tableViewCar == tableView) {
        return 30;
    }else if (_tableViewO == tableView || _tableViewT == tableView || _tableViewResult == tableView ||_tableViewTheme == tableView || _tableViewCarForum == tableView){
        return 0;
    }else{
        return 40;
    }
}
//设置各个分区顶部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    //判断是哪个tableview
    if (_tableViewCar == tableView) {
        ForumList *car = [self.letterArr objectAtIndex:section];
        NSString *str = car.letter;
        return str;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableViewResult == tableView || _tableViewTheme == tableView || _tableViewCarForum == tableView){
        return 40;
    }else if (_tableViewT == tableView){
        return 100;
    }else if (_tableViewCar == tableView){
        return 50;
    }
    
    return ((self.view.frame.size.height - 50 - 49) / 6);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //   点击首页上的cell时,进入到详情界面
    if (_tableViewO == tableView) {
        DetailViewController *detailVC = [[DetailViewController alloc]init];
        Forum *forum = [self.arr objectAtIndex:indexPath.row];
        detailVC.forum = forum;
        [self presentViewController:detailVC animated:YES completion:^{
        }];
        [detailVC release];
    }
    //点击侧边框上的cell的时候 更新首页信息
    if (_tableViewResult == tableView){
        [self changeCollection:indexPath.row];
        self.viewB.hidden = YES;
        self.tableViewO.alpha = 1;
    }
//    常用论坛界面tableViewT
    if (_tableViewT == tableView) {
//        判断是那个cell 从而进入不同的侧边栏
        if (0 == indexPath.row) {
            self.viewCar.hidden = NO;
            self.tableViewT.alpha = 0.1;
        }
        if (1 == indexPath.row) {
            self.viewTheme.hidden = NO;
            self.tableViewT.alpha = 0.1;
        }}
//     主题论坛界面tableviewTheme
    if (_tableViewTheme == tableView) {
        self.viewTheme.hidden = YES;
        CommentViewController *commentVC = [[CommentViewController alloc]init];
        Forum *foru = [self.arrTheme objectAtIndex:indexPath.row];
        self.tableViewT.alpha = 1;
        commentVC.foru = foru;
        [self presentViewController:commentVC animated:YES completion:^{
        }];
        [commentVC release];
        
//   车系论坛界面tableviewCar
    }
    if (_tableViewCar == tableView) {
        ForumList *list = [self.letterArr objectAtIndex:indexPath.section];
        ForumBrand *brand  = [list.arr objectAtIndex:indexPath.row];
        self.stringArr = [NSMutableArray array];
        self.stringIdArr = [NSMutableArray array];
        for (NSInteger i = 0; i < brand.seriesclub.count; i++) {
      //   取论坛名
            NSString *string = [[brand.seriesclub objectAtIndex:i]objectForKey:@"bbsname"];
            [self.stringArr addObject:string];
      //   取论坛id
           NSString *stringId = [[brand.seriesclub objectAtIndex:i]objectForKey:@"bbsid"];
            [self.stringIdArr addObject:stringId];
        }
        self.bbsnameNum = brand.seriesclub.count;
         [self layoutViewCarForum];
          self.viewCarForum.hidden = NO;
    }
    if (_tableViewCarForum == tableView) {
        DownViewController *downVC = [[DownViewController alloc]init];
        downVC.stringId = [self.stringIdArr objectAtIndex:indexPath.row];
        downVC.stringname = [self.stringArr objectAtIndex:indexPath.row];
        [self presentViewController:downVC animated:YES completion:^{
        }];
        [downVC release];
    }
}

#pragma mark - 主页面点击不同的名字切换不同的信息
- (void)changeCollection:(NSInteger)changeNum
{
    self.strID = self.arrId[changeNum];
    self.str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c%@-p1-s30.html", self.strID];
    [self datahander];
}

#pragma mark - segmentcontrol
- (void)clickSegmentcontrolAction:(UISegmentedControl *)segmentControl
{
    [self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width * self.segmentControl.selectedSegmentIndex, 0) animated:YES];
}

#pragma mark - scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.segmentControl.selectedSegmentIndex = scrollView.contentOffset.x / self.view.bounds.size.width;
}

#pragma mark - finderButton
- (void)clickFinderButton:(UIButton *)button
{
    NSLog(@"test");
    FinderViewController *finderVC = [FinderViewController alloc];
    [self presentViewController:finderVC animated:YES completion:^{
    }];
    [finderVC release];
}

#pragma mark - tableViewResult上的方法
- (void)clickCloseButton:(UIButton *)button
{
    self.viewB.hidden = YES;
    self.tableViewO.alpha = 1;
    self.viewTheme.hidden = YES;
    self.viewCar.hidden = YES;
    self.viewCarForum.hidden = YES;
    self.tableViewT.alpha = 1;
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
    UIView *view = swipe.view;
//    self.tableViewO.alpha = 1;
    self.tableViewT.alpha = 1;
    view.hidden = YES;
}
- (void)clickDownButton:(UIButton *)button
{
    self.viewB.hidden = NO;
//    self.tableViewO.alpha = 0.1;
    self.tableViewO.backgroundColor = [UIColor clearColor];
}
- (void)clickReButton:(UIButton *)button
{
    [self.view bringSubviewToFront:self.viewCar];
}
#pragma mark - 刷新的方法
- (void)addHeader:(UITableView *)header;
{
    __unsafe_unretained typeof(self) vc = self;
    // 添加下拉刷新头部控件
    [self.tableViewO addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block

            [self datahander];
        

        
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.tableViewO reloadData];
            // 结束刷新
            [vc.tableViewO headerEndRefreshing];
//            NSLog(@"KKkkk");
        });
    } dateKey:@"collection"];
// dateKey用于存储刷新时间，也可以不传值，可以保证不同界面拥有不同的刷新时间
  
}

- (void)addFooter:(UITableView *)footer
{
    __unsafe_unretained typeof(self) vc = self;
    // 添加上拉刷新尾部控件
    [self.tableViewO addFooterWithCallback:^{
        self.num = [self.pageIndex integerValue];
        self.num += 1;
        NSLog(@"self.num = %ld", self.num);
            if (self.strID == 0) {
                self.str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c0-p%ld-s30.html", self.num];
            }else{
             
                self.str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/club/jingxuantopic-a2-pm2-v4.6.1-c%@-p%ld-s30.html", self.strID, self.num];

            }
          [self datahander:self.str];
 
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.tableViewO reloadData];
            // 结束刷新
            [vc.tableViewO footerEndRefreshing];
            
        });
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
